/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.xs_book.service;

import java.util.Date;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.xs_book_other.entity.XsBookOther;
import com.jeeplus.modules.xs_book_other.mapper.XsBookOtherMapper;
import com.jeeplus.modules.xs_notice.entity.XsNotice;
import com.jeeplus.modules.xs_notice.mapper.XsNoticeMapper;
import com.jeeplus.modules.xs_notice_user.entity.XsNoticeUser;
import com.jeeplus.modules.xs_notice_user.mapper.XsNoticeUserMapper;
import com.jeeplus.modules.xs_user.mapper.XsUserMapper;
import com.jeeplus.modules.xs_writer.mapper.XsWriterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.xs_book.entity.XsBook;
import com.jeeplus.modules.xs_book.mapper.XsBookMapper;

/**
 * 书籍信息Service
 *
 * @author 天竹子
 * @version 2021-07-24
 */
@Service
@Transactional(readOnly = true)
public class XsBookService extends CrudService<XsBookMapper, XsBook> {

    @Autowired
    private XsNoticeUserMapper xsNoticeUserMapper;
    @Autowired
    private XsNoticeMapper xsNoticeMapper;
    @Autowired
    private XsUserMapper xsUserMapper;
    @Autowired
    private XsWriterMapper xsWriterMapper;
    @Autowired
    private XsBookOtherMapper xsBookOtherMapper;
    @Autowired
    private XsBookMapper xsBookMapper;

    public XsBook get(String id) {
        return super.get(id);
    }

    public List<XsBook> findList(XsBook xsBook) {
        return super.findList(xsBook);
    }

    public Page<XsBook> findPage(Page<XsBook> page, XsBook xsBook) {

        return super.findPage(page, xsBook);
    }

    @Transactional(readOnly = false)
    public void save(XsBook xsBook) {
        int i = 0;
        if (StringUtils.isNotBlank(xsBook.getId())) {
            i = 1;
            XsBook book = super.get(xsBook.getId());
            if (book != null) {
                if ("1".equals(xsBook.getIsShelves()) && "2".equals(book.getIsShelves())) {
                    XsNotice xsNotice = new XsNotice();
                    xsNotice.setType("4");
                    xsNotice.setDescribe("您有电子书上架啦");
                    xsNotice.setTitle("电子书上架通知");
                    xsNotice.setCreateDate(new Date());
                    xsNotice.setContent("<p>您的书籍《" + xsBook.getName() + "》已经上架了，请尽快查看</p>");
                    xsNoticeMapper.insert(xsNotice);
                    XsNoticeUser xsNoticeUser = new XsNoticeUser();
                    xsNoticeUser.setIsRead(2);
                    xsNoticeUser.setNoticeId(xsNotice.getId());
                    xsNoticeUser.setUser(xsUserMapper.get(xsWriterMapper.get(xsBook.getWriter()).getUser()));
                    xsNoticeUserMapper.insert(xsNoticeUser);
                }
            }
        }
        if(StringUtils.isBlank(xsBook.getCover())){
            xsBook.setCover("http://47.103.61.235:8007/images/2021-10-09/head.png");
        }
        super.save(xsBook);
        if (i == 0) {
            XsBookOther xsBookOther = new XsBookOther();
            xsBookOther.setBook(xsBook);
            xsBookOther.setClickAllNumber(0);
            xsBookOther.setClickMonthNumber(0);
            xsBookOther.setClickWeeksNumber(0);
            xsBookOther.setExceptionalAllNumber(0.0);
            xsBookOther.setExceptionalMonthNumber(0.0);
            xsBookOther.setExceptionalWeeksNumber(0.0);
            xsBookOtherMapper.insert(xsBookOther);
        }
    }

    @Transactional(readOnly = false)
    public void delete(XsBook xsBook) {
//        super.delete(xsBook);
//        super.mapper.deleteBook(xsBook.getId());
        xsBook.setDelFlag("1");
        xsBookMapper.deleteByLogic(xsBook);
    }

}